<%@ page import="stephen.com.constant.Const" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<c:set var="pageGroupCount" value="<%=Const.PAGE_GROUP_COUNT%>"/>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Bootstrap CSS-->
    <link rel="stylesheet" href="${basePath}/assets/vendor/bootstrap/css/bootstrap.css">
    <!-- Font Awesome CSS-->
    <link rel="stylesheet" href="${basePath}/assets/vendor/font-awesome/css/font-awesome.min.css">
    <!-- Custom Font Icons CSS-->
    <link rel="stylesheet" href="${basePath}/assets/css/font.css">
    <!-- theme stylesheet-->
    <link rel="stylesheet" href="${basePath}/assets/css/style.default.css" id="theme-stylesheet">
    <!-- Custom stylesheet - for your changes-->
    <link rel="stylesheet" href="${basePath}/assets/css/layer.css">
    <link rel="stylesheet" href="${basePath}/assets/bootstrap/css/bootstrap-switch.min.css">
    <link rel="stylesheet" href="${basePath}/assets/css/custom.css">
    <title>查看用户</title>
</head>
<body>
<%@ include file="samejsp/head.jsp" %>

<div class="d-flex align-items-stretch">
    <!-- Sidebar Navigation-->
    <nav id="sidebar">
        <!-- Sidebar Header-->
        <%@ include file="samejsp/sideHead.jsp"%>
        <!-- Sidebar Navidation Menus--><span class="heading">Main</span>
        <ul class="list-unstyled">
            <li><a href="${basePath}/view/home"> <i class="icon-home"></i>主页 </a></li>
            <li><a href="#userDropdown" data-toggle="collapse" aria-expanded="true">
                <i class="icon-windows"></i>用户列表</a>
                <ul id="userDropdown" class="collapse show">
                    <li class="active"><a href="${basePath}/users/userList">查看用户</a></li>
                    <li><a href="my_user.html">我关注的用户</a></li>
                    <li><a href="article.html">发布文章</a></li>
                    <li><a href="article_collect.html">我的收藏</a></li>
                </ul>
            </li>
            <!--<li><a href="login.html"> <i class="icon-logout"></i>Login page </a></li>-->

            <li><a href="#depDropdown" data-toggle="collapse"> <i class="icon-windows2"></i>部门列表</a>
                <ul id="depDropdown" class="collapse list-unstyled ">
                    <li><a href="department.html">全部部门</a></li>
                    <li><a href="meeting.html">会议系统</a></li>
                </ul>
            </li>

        </ul>

    </nav>
    <!-- Sidebar Navigation end-->
    <div class="page-content">
        <div class="page-header">
            <div class="container-fluid">
                <h2 class="h5 no-margin-bottom">用户列表</h2>
            </div>
        </div>
        <section class="no-padding-bottom">
            <div class="title">
                <form class="form-inline">
                    <div class="form-group">
                        <label for="inlineFormInput" class="sr-only">Name</label>
                        <input id="inlineFormInput" type="text" placeholder="按名字查找" class="mr-sm-3 form-control">
                    </div>
                    <div class="form-group">
                        <input type="submit" value="查询" class="btn btn-primary">
                    </div>
                </form>

            </div>
            <div class="table-responsive">
                <table class="table">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>姓名</th>
                            <th>性别</th>
                            <th>年龄</th>
                            <th>简介</th>
                            <th>操作</th>
                            <th>加关注</th>
                        </tr>
                    </thead>
                    <tbody>
                        <c:forEach items="${requestScope.userList}" var="user">
                            <tr>
                                <th scope="row">${user.userId}</th>
                                <td>${user.realName}</td>
                                <td>
                                    <c:if test="${user.gender == 0}">
                                        男
                                    </c:if>

                                    <c:if test="${user.gender == 1}">
                                        女
                                    </c:if>

                                </td>
                                <td>${user.age}</td>
                                <td>${user.introduce}</td>
                                <td>
                                    <input id="dUser" data-id="${user.userId}" type="submit" value="详细信息"
                                           class="btn-xs btn-primary userDetail">
                                </td>

                                <td>
                                    <c:if test="${user.ufId == null}">
                                        <input data-ufid="${user.ufId}" data-uid="${user.userId}" type="checkbox" value="" class="checkbox-template">
                                    </c:if>

                                    <c:if test="${user.ufId != null}">
                                        <input checked="checked" data-ufid="${user.ufId}" data-uid="${user.userId}" type="checkbox" value="" class="checkbox-template">
                                    </c:if>
                                </td>
                            </tr>
                        </c:forEach>

                    </tbody>
                </table>

                <nav class="text-center" aria-label="Page navigation">
                    <ul id="pageUL" class="pagination">
                        <li>
                            <a id="preGroup" href="#" aria-label="Previous"> <span aria-hidden="true"><<</span> </a>
                        </li>
                        <li>
                            <a id="preOne" href="#" aria-label="Previous"> <span aria-hidden="true"><</span> </a>
                        </li>
                        <%--begin 和 end 中存入的值是数字时就得到一个数字区间
                        var和step可以指定数字区间每次遍历的迭代属性--%>
                        <c:forEach var="index" varStatus="status" begin="${requestScope.startPage}" end="${requestScope.endPage}" step="1">
                            <c:if test="${status.index == requestScope.currentPage}">
                                <li data-page="${index}">
                                    <a class="active" href="${pageContext.request.contextPath}/view/user?page=${index}">${index}</a>
                                </li>
                            </c:if>

                            <c:if test="${status.index != requestScope.currentPage}">
                                <li data-page="${index}">
                                    <a href="${basePath}/view/user?page=${index}">${index}</a>
                                </li>
                            </c:if>

                        </c:forEach>
                        <li>
                            <a id="nextOne" href="#" aria-label="Next"> <span aria-hidden="true">></span> </a>
                        </li>
                        <li>
                            <a id="nextGroup" href="#" aria-label="Next"> <span aria-hidden="true">>></span> </a>
                        </li>
                    </ul>
                    <ul class="pagination">
                        <li class="searchPage">
                            <span><div>第 <input type="text" width="20"> 页 <a id="searchPageButton" href="#"> 转到 </a></div></span>
                        </li>
                    </ul>
                </nav>
            </div>
        </section>

        <%@ include file="samejsp/footer.jsp" %>
    </div>
</div>

<!-- JavaScript files-->
<script src="${basePath}/assets/vendor/jquery/jquery.min.js"></script>
<script src="${basePath}/assets/vendor/popper.js/umd/popper.min.js"></script>
<script src="${basePath}/assets/vendor/bootstrap/js/bootstrap.min.js"></script>
<script src="${basePath}/assets/bootstrap/js/bootstrap-switch.min.js"></script>
<script src="${basePath}/assets/vendor/jquery.cookie/jquery.cookie.js"></script>
<script src="${basePath}/assets/vendor/chart.js/Chart.min.js"></script>
<script src="${basePath}/assets/js/layer.js"></script>
<script src="${basePath}/assets/vendor/jquery-validation/jquery.validate.min.js"></script>
<script src="${basePath}/assets/js/charts-home.js"></script>
<script src="${basePath}/assets/js/front.js"></script>
<script src="${basePath}/assets/js/custom.js"></script>

<script>
    $(function () {
        $("#preOne").click(function () {
            var currentLi = $("#pageUL .active").parent();
            var currentPage = currentLi.attr("data-page");
            if ('1' === currentPage) {
                //提示框打印信息
                layer.msg("前面没有页码了");
                return;
            }
            window.location.href = '${basePath}/view/user?page=' + (Number(${requestScope.currentPage}) - 1);
        });

        $("#nextOne").click(function () {
            var currentLi = $("#pageUL .active").parent();
            var currentPage = currentLi.attr("data-page");

            if (currentPage >=${requestScope.sumPage}) {
                layer.msg("已经是最后一页了！");
                return;
            }
            window.location.href = '${basePath}/view/user?page=' + (Number(${requestScope.currentPage}) + 1);
        })
        $("#preGroup").click(function () {
            var li = $("#pageUL>li:nth-child(3)");
            var firstPage = li.attr("data-page");
            if ('1' === firstPage) {
                //提示框打印信息
                layer.msg("前面没有新的分组了！");
                return;
            }
            window.location.href = '${pageContext.request.contextPath}/view/user?page=' + (Number(firstPage) - ${pageGroupCount});
        });

        $("#nextGroup").click(function () {
            var li = $("#pageUL>li:nth-child(3)");
            var firstPage = li.attr("data-page");
            if (${requestScope.sumPage <= requestScope.endPage}) {
                layer.msg("后面没有新的分组了！");
                return;
            }
            window.location.href = '${basePath}/view/user?page=' + (Number(firstPage) + 5);
        })

        $("#searchPageButton").click(function () {
            var gotoPage = $(".searchPage input").val();
            console.log(gotoPage);
            if (null === gotoPage || gotoPage=="") {
                return;
            }
            if (parseInt(${requestScope.sumPage}) < gotoPage && gotoPage < 1) {
                layer.msg("已经是第一页了！");
                return;
            }
            window.location.href = '${basePath}/view/user?page=' + gotoPage;
        })

        $(".table").find("input[type='checkbox']").on("click", function () {
            if ($(this).prop("checked")) {
                var uId = $(this).attr("data-uid");
                if(uId === ${sessionScope.userId}){
                    //再点击一下，变回取消打钩
                    $(this).click();
                    layer.msg("不能自己关注自己哦~")
                }else {
                    $.post('${basePath}/user/focus', {uId: uId}, function (data) {
                        console.log("success" === data);
                        if ("success" === data) {
                            layer.msg("关注成功")
                        } else {
                            layer.msg(data)
                        }
                    });
                }
            } else {
                var ufId = $(this).attr("data-ufid");
                console.log($(this).attr("data-ufid"));
                //写ajax去根据这个ufid删除关注表的数据
                $.post('${basePath}/user/unFocus', {ufId: ufId}, function (data) {
                    console.log({ufId: ufId});
                    console.log("success" === data);
                    if ("success" === data) {
                        layer.msg("取关成功")
                    }
                });
            }

        })

        //用户列表，查看详情，通过class名字userDetail来找
        $(".userDetail").click(function () {
            console.log("你点击了用户详情");
            var userId = $(this).attr("data-id");
            window.location.href = '${pageContext.request.contextPath}/user/getDetail?userId=' + userId;
        });


    })
</script>

</body>
</html>